﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win.UltraWinGrid;

namespace CKNetFrameWork
{
    public partial class Frm_Order : DockContent
    {
        private int m_nDelIndex = -1;
        private bool m_bInsertFlag = false;
        private int m_OrderID = 0;
        private LSY.BLL.LSY_OrderTab m_bllLSY_OrderTab = new LSY.BLL.LSY_OrderTab();
        private LSY.BLL.LSY_SellerDetailTab m_bllLSY_SellerDetailTab = new LSY.BLL.LSY_SellerDetailTab();
        private LSY.BLL.LSY_SellerTab m_bllLSY_SellerTab = new LSY.BLL.LSY_SellerTab();
        private LSY.BLL.LSY_ProductTab m_bllLSY_ProductTab = new LSY.BLL.LSY_ProductTab();
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            ultraGrid1.DataSource = null;
            DataSet ds = m_bllLSY_OrderTab.GetListInfo(strWhere);

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;

        }

        public Frm_Order()
        {
            InitializeComponent();
        }

        private void Frm_Order_Load(object sender, EventArgs e)
        {

        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
     
        private void buttonNew_Click(object sender, EventArgs e)
        {

            try
            {
                LSY.Model.LSY_OrderTab modelLSY_OrderTab = new LSY.Model.LSY_OrderTab();
                int nNewBGPackTabID = m_bllLSY_OrderTab.GetMaxId();
                string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');

                modelLSY_OrderTab.LSY_OrderNo = strDate + nNewBGPackTabID.ToString();
                modelLSY_OrderTab.LSY_OrderSheetDate = System.DateTime.Now.Date;
                modelLSY_OrderTab.LSY_OrderState = 0;
                modelLSY_OrderTab.LSY_AgentTabID = Frm_Main.m_nLoginUserId;
                m_bllLSY_OrderTab.Add(modelLSY_OrderTab);
                ShowAllRecordInDataGridView("LSY_OrderTab.LSY_AgentTabID = '" + Frm_Main.m_nLoginUserId + "'");
                MessageBox.Show("添加新订单成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        private void cellBGPackingTabClick(int nSelectBGPackingTabID)
        {

            ShowAllMXRecordInDataGridView(nSelectBGPackingTabID);


        }
        private void ShowAllMXRecordInDataGridView(int nBGPackingTabID)
        {
            ultraGrid2.DataSource = null;
            DataSet m_ds = m_bllLSY_SellerDetailTab.GetList("LSY_SellerDetailTab.LSY_OrderTabid = '" + nBGPackingTabID + "'");
            
            //if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                DataSet ds = m_bllLSY_SellerTab.GetList("");

                this.ultraDropDownSeller.DataSource = ds.Tables[0];
                this.ultraDropDownSeller.DisplayMember = "零售商名称";
                this.ultraDropDownSeller.ValueMember = "LSY_SellerTabID";
 

                this.ultraDropDownSeller.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                for (int i = 0; i < this.ultraDropDownSeller.DisplayLayout.Bands[0].Columns.Count; i++)
                    this.ultraDropDownSeller.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                this.ultraDropDownSeller.DisplayLayout.Bands[0].Columns["零售商名称"].Hidden = false;

                ds = m_bllLSY_ProductTab.GetList("");

                this.ultraDropDownProduct.DataSource = ds.Tables[0];
                this.ultraDropDownProduct.DisplayMember = "产品名";
                this.ultraDropDownProduct.ValueMember = "LSY_ProductTabID";
                //this.ultraDropDownProduct.DisplayLayout.Bands[0].Columns[0].Hidden = true;

                this.ultraDropDownProduct.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                for (int i = 0; i < this.ultraDropDownProduct.DisplayLayout.Bands[0].Columns.Count; i++)
                    this.ultraDropDownProduct.DisplayLayout.Bands[0].Columns[i].Hidden = true;

                this.ultraDropDownProduct.DisplayLayout.Bands[0].Columns["产品名"].Hidden = false;


                ultraGrid2.DataSource = m_ds.Tables[0];
                ultraGrid2.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid2.DisplayLayout.Bands[0].Columns[1].Hidden = true;

                this.ultraGrid2.DisplayLayout.Bands[0].Columns["零售商"].ValueList = this.ultraDropDownSeller;
                this.ultraGrid2.DisplayLayout.Bands[0].Columns["产品名称"].ValueList = this.ultraDropDownProduct;
                
            }

        }
        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;


            string strBGPackingTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_OrderID = Convert.ToInt32(strBGPackingTabID, 10);
            cellBGPackingTabClick(m_OrderID);
          
        }

        private void ultraGrid2_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
        }

        private void ultraGrid2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {
              
                if (!m_bInsertFlag)
                {

                    UltraGridRow row = this.ultraGrid2.DisplayLayout.Bands[0].AddNew();
                    //row.ParentCollection.Move(row, 0);
                    //this.ultraGrid2.ActiveRowScrollRegion.ScrollRowIntoView(row);
                }
            }
            switch (e.KeyCode)
            {
                case Keys.Up:
                    {
                        string strTemp = this.ultraGrid2.ActiveCell.Column.Header.Caption;
                        if (strTemp == "单位")
                            return;
                        int nColumnIndex = this.ultraGrid2.ActiveCell.Column.Index;
                        int nIndex = ultraGrid2.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid2.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        string strTemp = this.ultraGrid2.ActiveCell.Column.Header.Caption;
                        if (strTemp == "单位")
                            return;
                        int nColumnIndex = this.ultraGrid2.ActiveCell.Column.Index;
                        int nIndex = ultraGrid2.ActiveRow.Index;
                        if (nIndex < ultraGrid2.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid2.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid2.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid2.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }
        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid2.Rows[nRowIndex];

                LSY.Model.LSY_SellerDetailTab modelLSY_SellerDetailTab = new LSY.Model.LSY_SellerDetailTab();

         
            
                modelLSY_SellerDetailTab.LSY_SellerDetailDate = System.DateTime.Now.Date;
                modelLSY_SellerDetailTab.LSY_OrderTabID = m_OrderID;
                modelLSY_SellerDetailTab.LSY_SellerBoxCount = Convert.ToInt32(CurrentUGR.Cells["产品数量"].Value.ToString(), 10);
                modelLSY_SellerDetailTab.LSY_ProductTabID = Convert.ToInt32(CurrentUGR.Cells["产品名称"].Value.ToString(), 10);
                modelLSY_SellerDetailTab.LSY_SellerTabID = Convert.ToInt32(CurrentUGR.Cells["零售商"].Value.ToString(), 10);

                int nId = m_bllLSY_SellerDetailTab.Add(modelLSY_SellerDetailTab);

                ShowAllRecordInDataGridView("LSY_OrderTab.LSY_OrderTabID = '" + m_OrderID + "'");
                ShowAllMXRecordInDataGridView(m_OrderID);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void InitNewAddRowValue(int nIndex)
        {

            UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];

            CurrentUGR.Cells["零售商"].Value = 1;
            CurrentUGR.Cells["产品名称"].Value = 1;
            CurrentUGR.Cells["产品数量"].Value = 0;
            if (m_bInsertFlag)
            {
                ultraGrid2.Rows[nIndex].Cells[0].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }
        private void ultraGrid2_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid2.Rows[e.Row.Index].Cells[2].IgnoreRowColActivation = true;
            ultraGrid2.Rows[e.Row.Index].Cells[2].Activation = Activation.AllowEdit;
            ultraGrid2.Rows[e.Row.Index].Cells[2].Activate();
            ultraGrid2.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            InitNewAddRowValue(e.Row.Index);
        }
        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid2.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                LSY.Model.LSY_SellerDetailTab modelLSY_SellerDetailTab = m_bllLSY_SellerDetailTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);



                modelLSY_SellerDetailTab.LSY_SellerDetailDate = System.DateTime.Now.Date;
                modelLSY_SellerDetailTab.LSY_OrderTabID = m_OrderID;
                modelLSY_SellerDetailTab.LSY_SellerBoxCount = Convert.ToInt32(CurrentUGR.Cells["产品数量"].Value.ToString(), 10);

                string strBGH = CurrentUGR.Cells["产品名称"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelLSY_SellerDetailTab.LSY_ProductTabID = Convert.ToInt32(CurrentUGR.Cells["产品名称"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllLSY_ProductTab.GetList("LSY_ProductName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelLSY_SellerDetailTab.LSY_ProductTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["LSY_ProductTabID"].ToString(), 10);
                    }
                }

                strBGH = CurrentUGR.Cells["零售商"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelLSY_SellerDetailTab.LSY_SellerTabID = Convert.ToInt32(CurrentUGR.Cells["零售商"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllLSY_SellerTab.GetList("LSY_SellerName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelLSY_SellerDetailTab.LSY_SellerTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["LSY_SellerTabID"].ToString(), 10);
                    }
                }

                m_bllLSY_SellerDetailTab.Update(modelLSY_SellerDetailTab);

                ShowAllRecordInDataGridView("LSY_OrderTab.LSY_OrderTabID = '" + m_OrderID + "'");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void ultraGrid2_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid2.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid2.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;



                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }


    }
}
